import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:flutter_widget/viewmodel/mvvm.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:provider/provider.dart';

// ignore: must_be_immutable
class MvvmPage extends StatefulWidget {
  String name;
  MvvmPage({Key? key, required this.name}) : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  _MvvmPageState createState() => _MvvmPageState();
}

class _MvvmPageState extends State<MvvmPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final List lists = Provider.of<MvvmProvider>(context).list; //获取state的值
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      body: Center(
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            final String categoryId = lists[index]['category_id'].toString();
            final String categoryName =
                lists[index]['category_name'].toString();
            return Padding(
              padding: const EdgeInsets.all(16.0),
              child: ListTile(
                leading: const Icon(Icons.ac_unit), // 左侧图标按钮
                title: Text(categoryId),
                // tileColor: Colors.grey,
                subtitle: Text(categoryName),
                trailing: const Icon(Icons.add), // 右侧图标按钮
              ),
            );
          },
          itemCount: lists.length,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Theme.of(context).primaryColor,
        child: const Icon(Icons.add),
        onPressed: () {
          context.read<MvvmProvider>().getList(); // 修改state的值
        },
      ),
    );
  }
}
